package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
//model layer package has to be imported

/**
 * Controller class for the address book
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressCtr
{
    
    //declare instance variables to reference containers
    private AddressBook addressBook;
    
    
    public AddressCtr()
    {
        addressBook = AddressBook.getInstance();
    }
    
    private Person findPerson(String phone)
    {
        return addressBook.getPerson(phone);
    }
    
    private Person createPerson(String name, String address, int postalCode, String city, String phone)
    {   
        //creates a Person object and stores it in the container
        Person person = null;
        boolean found = false;
        int i = 0;
        if(addressBook.getPersons().size() == 0)
        {
            person = new Person(name, address, postalCode, city, phone);
        }
        else
        {
            while(!found && i < addressBook.getPersons().size())
            {
                if(addressBook.getPersons().get(i).getPhone().equals(phone))
                {
                    found = true;
                }
                else
                {
                    i++;
                }
            }
            if(!found)
            {
                person = new Person(name, address, postalCode, city, phone);
            }
        }
        return person;
    }
    
    public void addPerson(String name, String address, int postalCode, String city, String phone)
    {
        addressBook.addPerson(createPerson(name, address, postalCode, city, phone));
    }
    
    public void deletePerson(String phone)
    {
        //search for the person and delete the object from the container
        addressBook.removePerson(phone);
    }
        
    public ArrayList<Person> listAllPersons()
    {
        return addressBook.getPersons();
    }
    
    public long findId(String phone)
    {
        return findPerson(phone).getId();
    }
    
    public String findName(String phone)
    {
        return findPerson(phone).getName();
    }
    
    public String findAddress(String phone)
    {
        return findPerson(phone).getAddress();
    }
    
    public int findPostalCode(String phone)
    {
        return findPerson(phone).getPostalCode();
    }
    
    public String findCity(String phone)
    {
        return findPerson(phone).getCity();
    }    
    
    public void changeName(String phone, String name)
    {
        findPerson(phone).setName(name);
    }
    
    public void changeAddress(String phone, String address)
    {
        findPerson(phone).setAddress(address);
    }
    
    public void changePostalCode(String phone, int postalCode)
    {
        findPerson(phone).setPostalCode(postalCode);
    }
    
    public void changeCity(String phone, String city)
    {
        findPerson(phone).setCity(city);
    }
    
    public void changePhone(String phone, String newPhone)
    {
        findPerson(phone).setPhone(newPhone);
    }
    
    public void printInfo(String phone)
    {
        addressBook.printInfo(phone);
    }
    
    public void printAllInfo()
    {
        addressBook.printPersons();
    }
}
